/**
 * 
 */
package de.deliancourt.jarvis.implementations;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.media.j3d.Texture2D;
import javax.vecmath.Point2f;
import javax.vecmath.Point3f;
import javax.vecmath.TexCoord2f;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import de.deliancourt.jarvis.interfaces.ModelLoader;

/**
 * @author Salacryl
 *
 */
public class ColladaLoader implements ModelLoader {

	
	DocumentBuilderFactory _factory = DocumentBuilderFactory.newInstance();
	DocumentBuilder _builder;
	Document _xml;
	Node _ImageLibrary;
	Node _GeometryLibrary;
	Point3f[] _verts;
	TexCoord2f[] _tcs;
	int[] _atr; 
	
	private ColladaLoader(){
		
	}
	
	public ColladaLoader(InputStream f){
		
		try {
			_builder = _factory.newDocumentBuilder();
			_xml= _builder.parse(f);
			_xml.normalize();
			_ImageLibrary = _xml.getElementsByTagName("library_images").item(0);
			_GeometryLibrary = _xml.getElementsByTagName("library_geometries").item(0);
		} catch (ParserConfigurationException | SAXException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private Node GetNodeByID(Node source){
		Node tmp=null;
		String stmp = source.getAttributes().getNamedItem("source").getNodeValue().substring(1);
		tmp =  _xml.getElementById(stmp);
		return tmp;
	}
	
	public void Load(){
		Node Geometries = _GeometryLibrary.getChildNodes().item(1);
		Node Mesh = Geometries.getChildNodes().item(1);
		float[] Vertlist= null;
		
		int count = 0;
		
		for(int i=1; i<Mesh.getChildNodes().getLength(); i++){
			Node tmp = Mesh.getChildNodes().item(i);
			if (tmp.getNodeType()== Node.ELEMENT_NODE){ 

				if (tmp.getAttributes().getNamedItem("id")!=null ||tmp.getNodeName().compareTo("polylist")==0 ){
					
					if (tmp.getAttributes().getNamedItem("id")!=null ){
						if (tmp.getAttributes().getNamedItem("id").getNodeValue().compareTo("Cube-mesh-positions")==0){
							Vertlist = GetValuesFloat(tmp.getChildNodes().item(1));
							_verts = new Point3f[Vertlist.length/3];
							for (int l=0;l<Vertlist.length;l=l+3){
								Point3f tv = new Point3f(Vertlist[l], Vertlist[l+1],Vertlist[l+2]);
								_verts[l/3]=tv;
							}
						}
					}
					if (tmp.getAttributes().getNamedItem("id")!=null ){
						if (tmp.getAttributes().getNamedItem("id").getNodeValue().compareTo("Cube-mesh-map-0")==0){
							Vertlist = GetValuesFloat(tmp.getChildNodes().item(1));
							_tcs = new TexCoord2f[Vertlist.length/2];
							for (int l=0;l<Vertlist.length;l=l+2){
								TexCoord2f tp = new TexCoord2f(Vertlist[l], Vertlist[l+1]);
								_tcs[l/2]=tp;
							}
						}
					}
					if (tmp.getNodeName().compareTo("polylist")==0){
						_atr = GetValuesInt(tmp.getChildNodes().item(9));
					}
				}
			}
		}
	
	}
	
	public int[] GetIndices(){
		return _atr;
	}
	private int GetCount(Node n){
		int count = 0;
		
		try {
			count = Integer.parseInt(n.getAttributes().getNamedItem("count").getNodeValue());
		} catch (NumberFormatException e){
			e.printStackTrace();
			return 0;
		}
		return count;
	}
	private float[] GetValuesFloat(Node n){
	//	int count = GetCount(n);
		float[] tmp = null;
		
		String[] sltmp=n.getFirstChild().getNodeValue().split(" ");
		tmp = new float[sltmp.length];
		for (int i =0;i< sltmp.length;i++){
			try {
				tmp[i] =Float.parseFloat(sltmp[i]);
			} catch (NumberFormatException e){
				e.printStackTrace();
			}
		}
		
		return tmp;
	}
	
	private int[] GetValuesInt(Node n){
		//int count = GetCount(n);
		int[] tmp = null;
		
		String[] sltmp=n.getFirstChild().getNodeValue().split(" ");
		tmp = new int[sltmp.length];
		for (int i =0;i< sltmp.length;i++){
			try {
				tmp[i] =Integer.parseInt(sltmp[i]);
			} catch (NumberFormatException e){
				e.printStackTrace();
			}
		}
		
		return tmp;
	}
	
	/* (non-Javadoc)
	 * @see de.deliancourt.jarvis.interfaces.ModelLoader#GetVerticesCoord()
	 */
	@Override
	public Point3f[] GetVerticesCoord() {
		// TODO Auto-generated method stub
		return _verts;
	}

	/* (non-Javadoc)
	 * @see de.deliancourt.jarvis.interfaces.ModelLoader#GetTextCood()
	 */
	@Override
	public TexCoord2f[] GetTextCood() {
		// TODO Auto-generated method stub
		return _tcs;
	}

	/* (non-Javadoc)
	 * @see de.deliancourt.jarvis.interfaces.ModelLoader#GetTexture()
	 */
	@Override
	public String GetTextureName() {
		// TODO Auto-generated method stub
		return _ImageLibrary.getChildNodes().item(1).getChildNodes().item(1).getFirstChild().getNodeValue();
	}

}
